package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wxm.servlet.BaseServlet;
import com.xm.dao.ImageDaoImpl;
import com.xm.entity.Banners;
import com.xm.entity.Images;
import com.xm.uti.Result;

/**
 * Servlet implementation class ImagesServletAdmin
 */
@WebServlet("/admin/ImagesServletAdmin")
public class ImagesServletAdmin extends BaseServlet {
	ImageDaoImpl imgsDao = new ImageDaoImpl();
	
	/**
	 * 产品详情图片（根据产品id单查相关的详情图片）
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取页面传来的参数
		String pid=req.getParameter("pid");
		// 1、调用dao层方法获取数据，
		ArrayList<Images> imgList = imgsDao.ProImageList(pid);
		// 将数据封装为layui需要的格式
		String res = Result.toClient(0, "产品详情图片", imgList);

		// 2、讲数据返回到页面上
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}
	

	/**
	 * 产品详情图片（全查）
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、调用dao层方法获取数据，
		ArrayList<Images> imgList = imgsDao.getImage();
		// 将数据封装为layui需要的格式
		String res = Result.toClient(0, "产品详情图片", imgList, imgList.size());

		// 2、讲数据返回到页面上
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	/**
	 * 添加方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insertImages(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、从页面上拿到数据
		String url = req.getParameter("url");// 表单中标签name的值
		String productid = req.getParameter("productid");

		// 2、调用dao层方法，将数据添加到数据库中
		Images images = new Images(0, url, Integer.valueOf(productid));
		boolean res = imgsDao.insertImage(images);

		// 3、讲数据返回到页面上
		PrintWriter out = resp.getWriter();

		if (res) {
			out.write(Result.toClient(0, "添加成功"));
		} else {
			out.write(Result.toClient(1, "添加失败"));
		}
		// 将数据封装为layui需要的格式
		out.flush();
		out.close();
	}

	/**
	 * 修改方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updImages(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1、从页面上拿到数据
		String url = req.getParameter("url");// 表单中标签name的值
		String productid = req.getParameter("productid");
		String id = req.getParameter("id");

		// 2、调用dao层方法，将数据添加到数据库中
		Images images = new Images(Integer.valueOf(id), url, Integer.valueOf(productid));
		boolean res = imgsDao.updateImage(images);

		// 3、讲数据返回到页面上
		PrintWriter out = resp.getWriter();

		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}
		// 将数据封装为layui需要的格式
		out.flush();
		out.close();
	}

	/**
	 * 删除方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delImagesById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取客户端传过来的参数
		String id = req.getParameter("id");
		if (id != null && !id.equals("")) {
			int id2 = Integer.valueOf(id);// 将字符串转换为整数类型
			// 2.调用dao层的方法，删除
			boolean res = imgsDao.delImage(id2);
			// 3、讲数据返回到页面上
			PrintWriter out = resp.getWriter();

			if (res) {
				out.write(Result.toClient(0, "删除成功"));
			} else {
				out.write(Result.toClient(1, "删除失败"));
			}
			// 将数据封装为layui需要的格式
			out.flush();
			out.close();
		}
	}
	
}
